import { defineStore } from 'pinia';
import { store } from '@/store';
import { deptTreeList } from '@/api/auth/dept';
export interface SystemState {
  deptTree: Recordable[];
}

export const useSystemStore = defineStore({
  id: 'app-system',
  state: (): SystemState => ({
    deptTree: [],
  }),
  getters: {
    getDeptTree(): Recordable[] {
      return this.deptTree;
    },
  },
  actions: {
    setDeptTree(deptTree: Recordable[]) {
      this.deptTree = deptTree;
    },

    // 获取部门信息
    GetDeptInfo() {
      return new Promise((resolve, reject) => {
        deptTreeList()
          .then((res) => {
            console.log(res);
            this.setDeptTree(res);
            resolve(res);
          })
          .catch((error) => {
            reject(error);
          });
      });
    },
  },
});

// Need to be used outside the setup
export function useSystemStoreWidthOut() {
  return useSystemStore(store);
}
